$S.ToolBar = Ext.extend($S.ViewComponent,{
//	constructor: function(config) {
//        $S.ToolBar.superclass.constructor.call(this, config);        
//    },
//    initComponent : function(){
//    	$S.ToolBar.superclass.initComponent.call(this);    	
//    },
//    initEvents : function(){
//    	$S.ToolBar.superclass.initEvents.call(this); 
//    }
})

$S.GridNavBar = Ext.extend($S.ToolBar,{
    initComponent : function(config){
        $S.GridNavBar.superclass.initComponent.call(this, config);
        this.dataSet = $(this.bind);
        this.info = this.wrap.child('div.info');
    },
    processListener: function(ou){
        $S.GridNavBar.superclass.processListener.call(this,ou);
        this.dataSet[ou]('load', this.onDataSetLoad,this);
    },
    onDataSetLoad : function(){
        var html=[],ds=this.dataSet,currentPage=ds.currentPage,totalCount=ds.totalCount,pageSize = ds.psize;
        html.push('第 <b>'+ds.currentIndex+'</b>');
        html.push(' - ');
        if(totalCount>currentPage*pageSize) {
            html.push('<b>'+currentPage*pageSize+'</b>');
        }else {
            html.push('<b>'+totalCount+'</b>');
        }
        html.push(' 条,');
        html.push(' 共 ' + '<b>'+totalCount+'</b> 条');
        this.info.update(html.join(''))
    }
})

$S.NavBar = Ext.extend($S.ToolBar,{
//	constructor: function(config) {
//        $S.NavBar.superclass.constructor.call(this, config);        
//    },
    initComponent : function(config){
        $S.NavBar.superclass.initComponent.call(this, config);
    	this.dataSet = $(this.dataSet);
    	this.navInfo = this.wrap.child('div[stype=displayInfo]');//Ext.get(this.infoId);
        if(this.navType != "simple"){
        	this.pageInput = $(this.inputId);
        	this.currentPage = this.wrap.child('div[stype=currentPage]');
        	this.pageInfo = this.wrap.child('div[stype=pageInfo]');//Ext.get(this.pageId);
        	this.pageInput.setValue(1);
        	
        	if(this.comboBoxId){
        		var pageSize=[10,20,50,100];
        		if(pageSize.indexOf(this.dataSet.psize)==-1){
        			pageSize.unshift(this.dataSet.psize);
        			pageSize.sort(function(a,b){return a-b});
        		}
        		var datas=[];
        		while(Ext.isDefined(pageSize[0])){
        			var ps=pageSize.shift();
        			datas.push({'code':ps,'name':ps});
        		}
        		var dataset=new $S.DataSet({'datas':datas});
    	    	this.pageSizeInput = $(this.comboBoxId);
    	    	this.pageSizeInput.setOptions(dataset);
    	    	this.pageSizeInput.setValue(this.dataSet.psize);
        		this.pageSizeInfo = this.wrap.child('div[stype=pageSizeInfo]');
        		this.pageSizeInfo2 = this.wrap.child('div[stype=pageSizeInfo2]');
        		this.pageSizeInfo.update('每页显示');//TODO:多语言
        		this.pageSizeInfo2.update('条');
        	}
        	this.currentPage.update('页数');
        }
    },
    processListener: function(ou){
    	$S.NavBar.superclass.processListener.call(this,ou);
    	this.dataSet[ou]('load', this.onLoad,this);
        if(this.navType != "simple"){
        	this.pageInput[ou]('keydown', this.onInputKeyPress, this);
        	if(this.pageSizeInput){
        		this.pageSizeInput[ou]('select', this.onInputSelect, this);
        	}
        }
    },
    initEvents : function(){
    	$S.NavBar.superclass.initEvents.call(this);
    },
    onLoad : function(){
    	this.navInfo.update(this.creatNavInfo());
        if(this.navType != "simple"){
        	this.pageInput.setValue(this.dataSet.currentPage);
        	this.pageInfo.update('共' + this.dataSet.totalPage + '页');            
        }
    },
    creatNavInfo : function(){
        if(this.navType == "simple"){
            var html=[],ds=this.dataSet,currentPage=ds.currentPage,totalPage=ds.totalPage;
            var from = ((this.dataSet.currentPage-1)*this.dataSet.psize+1);
            var to = this.dataSet.currentPage*this.dataSet.psize;
            if(totalPage){
                //html.push('<span>共'+totalPage+'页</span>');
                //html.push(currentPage == 1 ? '<span>首页</span>' : this.createAnchor('首页',1));
                html.push('<span style="margin-right:10px;">共 '+this.dataSet.totalCount+' 条记录</span>');
                html.push(currentPage == 1 ? '<span >上一页</span>' : this.createAnchor('上一页',currentPage-1));
                for(var i = 1 ; i < 4 && i <= totalPage ; i++){
                    html.push(i == currentPage ? '<b>' + currentPage + '</b>' : this.createAnchor(i,i));
                }
                if(totalPage > this.maxPageCount){
                    if(currentPage > 5)this.createSplit(html);
                    for(var i = currentPage - 1;i < currentPage + 2 ;i++){
                        if(i > 3 && i < totalPage - 2){
                            html.push(i == currentPage ? '<b>' + currentPage + '</b>' : this.createAnchor(i,i));
                        }
                    }
                    if(currentPage < totalPage - 4)this.createSplit(html);
                }else if(totalPage > 6){
                    for(var i = 4; i < totalPage - 2 ;i++){
                        html.push(i == currentPage ? '<b>' + currentPage + '</b>' : this.createAnchor(i,i));
                    }
                }
                for(var i = totalPage - 2 ; i < totalPage + 1; i++){
                    if(i > 3){
                        html.push(i == currentPage ? '<b>' + currentPage + '</b>' : this.createAnchor(i,i));
                    }
                }
                html.push(currentPage == totalPage ? '<span>下一页</span>' : this.createAnchor('下一页',currentPage+1));
                //html.push(currentPage == totalPage ? '<span>尾页</span>' : this.createAnchor('尾页',totalPage));
            }
            return html.join('');
        }else{
        	var from = ((this.dataSet.currentPage-1)*this.dataSet.psize+1);
        	var to = this.dataSet.currentPage*this.dataSet.psize;
        	if(to>this.dataSet.totalCount) to = this.dataSet.totalCount;
        	if(to==0) from =0;
        	return '显示' + from + ' - ' + to + ' '+ '共' + this.dataSet.totalCount + ' 条';
        }
    },
    createAnchor : function(text,page){
        return '<a href="javascript:$(\''+this.dataSet.id+'\').page('+page+')">'+text+'</a>';
    },
    createSplit : function(html){
        html.push('<span>···</span>');
    },
    onInputKeyPress : function(input, e){
    	if(e.keyCode == 13){
    		var page = parseInt(input.getRawValue());
    		if(isNaN(page)){
    			input.setValue(this.dataSet.currentPage);
    		}else{
    			if(page>0 && page<=this.dataSet.totalPage) {
    				this.dataSet.page(page);
    			}else{
    				input.setValue(this.dataSet.currentPage);
    			}
    		}
    	}    	
    },
    onInputSelect : function(combo,value){
    	this.dataSet.psize=value;
    	this.dataSet.query();
    }
})